package com.von.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.von.eduservice.entity.EduSubject;
import com.von.eduservice.entity.excel.SubjectData;
import com.von.eduservice.entity.subject.OneSubject;
import com.von.eduservice.entity.subject.TwoSubject;
import com.von.eduservice.listener.SubjectExcelListener;
import com.von.eduservice.mapper.EduSubjectMapper;
import com.von.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-05-13
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //课程分类列表
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //1. 查询所有一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        //Service中如何调用mapper?
        //1. 直接用service中的方法
        //this.list(wrapperOne)
        //2. 在ServiceImpl中已自动注入了basemapper，直接使用它的方法即可
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        //2.查询所欲二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        //创建集合，用于封装数据
        List<OneSubject> finalSubjectList = new ArrayList();


        //3.封装一级分类
        for (int i = 0; i < oneSubjectList.size();i++){
            EduSubject eduSubject = oneSubjectList.get(i);
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            BeanUtils.copyProperties(eduSubject,oneSubject);

            //4.封装二级分类
            List<TwoSubject> finalTwoList = new ArrayList();
            for(int j = 0; j<twoSubjectList.size();j++){
                EduSubject tSubject = twoSubjectList.get(j);
                TwoSubject twoSubject = new TwoSubject();
                if(tSubject.getParentId().equals(eduSubject.getId())){
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    finalTwoList.add(twoSubject);
                }
            oneSubject.setChildren(finalTwoList);
            }


            finalSubjectList.add(oneSubject);
        }



        return finalSubjectList;
    }
}
